<?php
if (!defined('MODULE_FILE')){
	die ("cant access file directly!");
}

$mid 	= ( ( true == isset( $_GET['mid'] ) && true == valStr( $_GET['mid'] ) ) ? $_GET['mid'] : NULL );
$smid 	= ( ( true == isset( $_GET['smid'] ) && true == valStr( $_GET['smid'] ) ) ? $_GET['smid'] : NULL );

autoLoader();

// display( $_REQUEST );
// display( $_POST ); exit;

if( false == isset( $_POST['Submit'] ) || false == valStr( $_POST['temp_master_id'] ) ) { 
	$str = '';
	$str .= '<form name=myform method=post action=modules.php?file=templates_master&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
	$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
	echo $str;
	exit;
}

if( true == isset( $_POST['operation'] ) && 'insert' == strtolower( $_POST['operation'] ) ) {
	
	$objBaseTemplateMaster 	= new BaseTemplateMaster();
	
	$objBaseTemplateMaster->setName( $_POST['name'] );
	$objBaseTemplateMaster->setDescription( $_POST['description'] );
	$objBaseTemplateMaster->setData( $_POST['message'] );
	$objBaseTemplateMaster->setTemplateTypeId( $_POST['temp_type_id'] );
	$objBaseTemplateMaster->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objBaseTemplateMaster->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objBaseTemplateMaster->setUpdatedOn( 'now()' );
	if( 1 == $objBaseTemplateMaster->insert() ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=view_templates_master&type=view&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
} else if( true == isset( $_POST['operation'] ) && 'update' == strtolower( $_POST['operation'] ) ) {
	$objTemplateMaster 		= new TemplateMaster();
	$objBaseTemplateMaster 	= $objTemplateMaster->fetchTemplateMasterById( $_POST['temp_master_id'] );
	
	$objBaseTemplateMaster->setId( $_POST['temp_master_id'] );
	
	if( true == valStr( $_POST['name'] ) ) {
		$objBaseTemplateMaster->setName( $_POST['name'] );
	}
	
	if( true == valStr( $_POST['description'] ) ) {
		$objBaseTemplateMaster->setDescription( $_POST['description'] );
	}
		
	if( true == valStr( $_POST['message'] ) ) {
		$objBaseTemplateMaster->setData( $_POST['message'] );
	}
	if( true == valStr( $_POST['temp_type_id'] ) ) {
		$objBaseTemplateMaster->setTemplateTypeId( $_POST['temp_type_id'] );
	}
	$objBaseTemplateMaster->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objBaseTemplateMaster->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objBaseTemplateMaster->setUpdatedOn( 'now()' );
	if( 1 == $objBaseTemplateMaster->update() ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=view_templates_master&type=view&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
}
?>